package com.ie.sevice.admin;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.comment.http.ResponseResult;
import com.ie.comment.sercurity.utiles.MyUtil;
import com.ie.entity.CartEntity;
import com.ie.entity.GoodsEntity;
import com.ie.mapper.admin.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hp
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper , GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;




    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        Integer currentPage = goodsEntity.getCurrentPage();
        if (currentPage == null || currentPage < 1){
            goodsEntity.setCurrentPage(1);
        }
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if (goodstypeId != null && goodstypeId != 0){
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }
        if (gname != null && !gname.isEmpty()){
            lambdaQueryWrapper.like(GoodsEntity::getGname, gname);
        }
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(iPage, lambdaQueryWrapper);

        HashMap<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());


        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {

        List<GoodsEntity> listAdv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId).last("limit 5").list();


        return ResponseResult.getSuccessResult(listAdv);
    }

    @Override
    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {

        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));

    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {

        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();

        if(goodsEntity.getGoodstypeId() != null && goodsEntity.getGoodstypeId() != 0){
           lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }

        if (gname != null){
            String trimmedGname = gname.trim();
            if (trimmedGname.isEmpty()){
                    lambdaQueryWrapper.like(GoodsEntity::getGname,  trimmedGname);
            }
            lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        }

        List<GoodsEntity> listIndex = list(lambdaQueryWrapper);

        return ResponseResult.getSuccessResult(listIndex);


    }

    @Override
    public ResponseResult<Map<String, Object>> addGoods(GoodsEntity goodsEntity) {
        byte[] logoFile = goodsEntity.getLogoFile();
        if (logoFile != null && logoFile.length > 0) {

            String path = "F:\\ebusiness-vue\\src\\assets";
            String fileName = MyUtil.getNewFileName(goodsEntity.getFileName());
            File filePath = new File(path + File.separator + fileName);
            if (!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                fileOutputStream.write(logoFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            goodsEntity.setGpicture(fileName);
        }
        if ("add".equals(goodsEntity.getAct())){
           boolean success =  save(goodsEntity);
            if (success){
                return ResponseResult.getMessageResult(null ,"A001");
            }else {
                return ResponseResult.getMessageResult(null ,"A002");
            }
        }else {
            boolean success = updateById(goodsEntity);
            if (success){
                return ResponseResult.getMessageResult(null ,"A001");
            }else {
                return ResponseResult.getMessageResult(null ,"A002");
            }
        }

    }

    @Override
    public ResponseResult<Map<String, Object>> deleteGoods(GoodsEntity goodsEntity) {

        Integer goodsId  = goodsEntity.getId();
        long res1 = 0;
        long res2 = 0;
        long res3 = 0;

        /**
         * res1 , res2 , res3
         * */
        if (res1 > 0 || res2 > 0 || res3 > 0){
            return ResponseResult.getMessageResult(null ,"A001");
        }
        boolean result = removeById(goodsId);
        if (result){
            return ResponseResult.getMessageResult(null ,"A002");
        }else {
            return ResponseResult.getMessageResult(null ,"A003");
        }

    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCarGoods(CartEntity cartEntity) {
        return null;
    }

    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        return null;
    }
}
